------------------ fgui require start ------------------
local GuildDetailSkin = require 'View/UIGuild/Skin/GuildDetailSkin';
local GuildCreateSkin = require 'View/UIGuild/Skin/GuildCreateSkin';
------------------  fgui require end  ------------------
local UIGuildRecommondSkin = Class("UIGuildRecommondSkin");
local GuildListItem = require 'View/UIGuild/Skin/GuildListItemSkin'

function UIGuildRecommondSkin:ctor(com)
    ------------------ fgui事件绑定start ------------------
    self:bindChild(com);
    self.ctl_details.onChanged:Add(self.onChange_Ctl_details, self);
    self.ctl_createGuild.onChanged:Add(self.onChange_Ctl_createGuild, self);
    self.guildlist.onChanged:Add(self.onChange_Guildlist, self);
    self.btn_blackbackground.onClick:Add(self.onTouch_Btn_blackbackground, self);
    self.btn_return.onClick:Add(self.onTouch_Btn_return, self);
    self.btn_search.onClick:Add(self.onTouch_Btn_search, self);
    self.btn_refresh.onClick:Add(self.onTouch_Btn_refresh, self);
    self.btn_createaguild.onClick:Add(self.onTouch_Btn_createaguild, self);
    self.dynamicList_Guildlist.onClickItem:Add(self.onClickItem_dynamicList_Guildlist, self);
    self.dynamicList_Guildlist.itemRenderer = function(index, obj)
        self:onRender_dynamicList_GuildlistItem(index, obj);
    end;
    self:init();
    ------------------  fgui事件绑定end  ------------------
end

function UIGuildRecommondSkin:bindChild(com)
    ------------------ fgui变量绑定start ------------------
    self.fguiObj = com;
    self.ctl_details = com:GetController("Ctl_details");
    self.ctl_createGuild = com:GetController("Ctl_createGuild");
    self.ctl_haveGuild = com:GetController("Ctl_haveGuild");
    self.guildlist = com:GetController("Guildlist");
    self.btn_blackbackground = com:GetChild("Btn_blackbackground");
    self.com_basemap = com:GetChild("Com_basemap");
    self.btn_return = com:GetChild("Btn_return");
    self.btn_search = com:GetChild("Btn_search");
    self.btn_refresh = com:GetChild("Btn_refresh");
    self.btn_createaguild = com:GetChild("Btn_createaguild");
    self.com_guildDetail = GuildDetailSkin.New(com:GetChild("Com_guildDetail"));
    self.com_createaguild = GuildCreateSkin.New(com:GetChild("Com_createaguild"));
    self.text_title = com:GetChild("Text_title");
    self.text_Tip = com:GetChild("Text");
    self.input_guildName = com:GetChild("Input_guildName");
    self.text_guildName = com:GetChild("Text_guildName");
    self.text_grade = com:GetChild("Text_grade");
    self.text_numberofpeople = com:GetChild("Text_numberofpeople");
    self.text_activity = com:GetChild("Text_activity");
    self.dynamicList_Guildlist = com:GetChild("dynamicList_Guildlist");
    ------------------  fgui变量绑定end  ------------------
end

function UIGuildRecommondSkin:OnDestroy()
    ------------------ fgui事件移除start ------------------
    self:beforeDestroy();
    self.ctl_details.onChanged:Clear();
    self.ctl_createGuild.onChanged:Clear();
    self.guildlist.onChanged:Clear();
    self.btn_blackbackground.onClick:Clear();
    self.btn_return.onClick:Clear();
    self.btn_search.onClick:Clear();
    self.btn_refresh.onClick:Clear();
    self.btn_createaguild.onClick:Clear();
    self.com_guildDetail:OnDestroy();
    self.com_createaguild:OnDestroy();
    self.dynamicList_Guildlist.onClickItem:Clear();
    self.dynamicList_Guildlist.itemRenderer = nil;
    ------------------  fgui事件移除end  ------------------
end

function UIGuildRecommondSkin:init()
    self.text_title.text = getLanguage("GuildInvestment_1033");
    self.btn_refresh.text = getLanguage("GuildInvestment_1041");
    self.btn_createaguild.text = getLanguage("GuildInvestment_1040");
    self.text_guildName.text = getLanguage("GuildInvestment_1036");
    self.input_guildName.promptText = getLanguage("GuildInvestment_1034");
    self.text_Tip.visible = false;  --策划需求先不让点，所以代码隐藏提示
    self.closeCallback = nil;
    self.unionOutlineInfos = nil;
    self.listItems = {}
    self.com_createaguild:setCloseCallback(function()
        self:hideCreateguild()
    end);
    self.com_guildDetail:setCloseCallback(function()
        self:hideGuildDetail()
    end);
    self.com_guildDetail:setHideDetailCallback(function()
        self:hideOnlyGuildDetail()
    end);
    ListenEvent(Config.EventType.Union_InfoList_S2C, self.respUnionInfoList, self);
    ListenEvent(Config.EventType.Union_DetailInfo_S2C, self.respDeatilUnionInfo, self);
    ListenEvent(Config.EventType.Union_SearchInfoList_S2C, self.respSearchUnionInfoList, self);
end

function UIGuildRecommondSkin:hideCreateguild()
    self.ctl_createGuild.selectedIndex = 0;
    if logicMgr.GuildManager.unionInfo and self.closeCallback then
        self.closeCallback();
    end
end

function UIGuildRecommondSkin:hideGuildDetail()
    self.ctl_details.selectedIndex = 0;
    if logicMgr.GuildManager.unionInfo and self.closeCallback then
        self.closeCallback();
    end
end

function UIGuildRecommondSkin:hideOnlyGuildDetail()
    self.ctl_details.selectedIndex = 0;
end

function UIGuildRecommondSkin:setCloseCallback(func, target)
    self.closeCallback = func;
end

function UIGuildRecommondSkin:updateUnionInfoList()
    local size = self.unionOutlineInfos and #self.unionOutlineInfos or 0;
    self.dynamicList_Guildlist.numItems = size;
    utimer.delayCall(function ()
        HelperFunc.ListTranstionPlay(self.dynamicList_Guildlist)
    end ,100)
    self.ctl_haveGuild.selectedIndex = 0
    if logicMgr.GuildManager.unionInfo then
        self.ctl_haveGuild.selectedIndex = 1    --自己有工会时不显示申请按钮
    end
    self.guildlist.selectedIndex = size > 0 and 1 or 0;
end

function UIGuildRecommondSkin:onChange_Ctl_details(event)
end

function UIGuildRecommondSkin:onChange_Ctl_createGuild(event)
end

function UIGuildRecommondSkin:onTouch_Btn_blackbackground(obj)
    if self.closeCallback then
        self.closeCallback();
    end
end

function UIGuildRecommondSkin:onTouch_Btn_return(obj)
    if self.closeCallback then
        self.closeCallback();
    end
end

function UIGuildRecommondSkin:onTouch_Btn_search(obj)
    local search = string.trim(self.input_guildName.text);

    if search ~= "" and logicMgr.ChatManager.HasFilterBlockWord(search) then
        UISysTips.AddMsg(getLanguage("UIFriend_ApplyMsgBlock"))
        return ;
    end

    if #search > 0 then
        local unionId = tonumber(search) or -1;
        unionHandler.reqSearchUnion(unionId, search);
    end
end

function UIGuildRecommondSkin:onTouch_Btn_refresh(obj)
    unionHandler.reqUnionInfoList();
end

function UIGuildRecommondSkin:onTouch_Btn_createaguild(obj)
    self.ctl_createGuild.selectedIndex = 1;
end

function UIGuildRecommondSkin:onClickItem_dynamicList_Guildlist(context)
    --if logicMgr.GuildManager.canClick then
    --    local item = self.listItems[context.data];
    --    unionHandler.reqUnionByIdInfo(item:getUnionId());
    --end

end

function UIGuildRecommondSkin:onRender_dynamicList_GuildlistItem(index, obj)
    local item = self.listItems[obj];
    if item == nil then
        item = GuildListItem.New(obj);
        self.listItems[obj] = item;
    end
    item.alpha = 0;
    item:onRender(self.unionOutlineInfos[index + 1]);
end
function UIGuildRecommondSkin:onChange_Guildlist(event)
end

------------------  ↑ fgui事件句柄 ↑  ------------------

function UIGuildRecommondSkin:respUnionInfoList(msg)
    if msg.state == 0 then
        self.unionOutlineInfos = msg.unionOutlineInfos;
        --进行排序 LBS一致的前置
        --local sortTopTable = {}
        --local sortOtherTable = {}
        --for k, v in pairs(self.unionOutlineInfos) do
        --    if logicMgr.LBSManager.GetLocationName() == v.unionSetting.province then
        --        table.insert(sortTopTable, v);
        --    else
        --        table.insert(sortOtherTable, v);
        --    end
        --end
        --self.unionOutlineInfos = sortTopTable;
        --for i = 1, #sortOtherTable do
        --    table.insert(self.unionOutlineInfos, sortOtherTable[i]);
        --end
        table.sort(self.unionOutlineInfos, function(a, b)
            if a.flag ~= b.flag then return a.flag < b.flag end
            return a.lv > b.lv;
        end)
        dump(self.unionOutlineInfos, "战团推荐列表:", 10)
        self:updateUnionInfoList();
    else

    end
end

function UIGuildRecommondSkin:respDeatilUnionInfo(msg)
    logicMgr.GuildManager.canClick = true
    if msg.unionInfo then
        self.ctl_details.selectedIndex = 1;
        self.com_guildDetail:upateView(msg.unionInfo);
    end
end

function UIGuildRecommondSkin:respSearchUnionInfoList(msg)
    if msg.state == 0 and #msg.unionOutlineInfos > 0 then
        self.unionOutlineInfos = msg.unionOutlineInfos;
        self:updateUnionInfoList();
    else
        self.input_guildName.text = "";
        UISysTips.AddMsg(getLanguage("GuildInvestment_1084"));
    end
end

function UIGuildRecommondSkin:beforeDestroy()
    self.closeCallback = nil;
    self.listItems = {};
    CloseEvent(Config.EventType.Union_InfoList_S2C, self.respUnionInfoList, self);
    CloseEvent(Config.EventType.Union_DetailInfo_S2C, self.respDeatilUnionInfo, self);
    CloseEvent(Config.EventType.Union_SearchInfoList_S2C, self.respSearchUnionInfoList, self);
end

return UIGuildRecommondSkin;
